import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:management/utils/device_info.dart';
import 'package:management/utils/log_utils.dart';
import 'package:management/views/bluetooth/bluetooth.dart';
import 'package:management/views/home/home_page.dart';
import 'package:management/views/mine/setting/setting.dart';
import 'package:management/views/mine/versionInfo.dart';
import 'package:management/views/mine/addDevice/faceCamera.dart';
import 'package:management/views/mine/videoIntercom/video_intercom.dart';
import 'package:management/views/pages/login_page.dart';
import 'package:management/views/pages/register_page.dart';
import 'package:management/views/mine/addDevice/add_device.dart';
import 'package:management/views/mine/userInfo/open_gate_record.dart';
import 'package:management/views/mine/speech_recognize.dart';
import 'package:management/views/mine/userInfo/user_info.dart';
import 'package:management/views/visitor/package/express_receipt.dart';
import 'package:management/views/visitor/query/delete_record_by_time.dart';
import 'package:management/views/visitor/query/query_result.dart';
import 'package:management/views/visitor/checkIn/reason_for_the_visit.dart';
import 'package:management/views/visitor/query/visitor_record_query.dart';
import 'views/mine/userInfo/search_gate_record_condition.dart';
import 'views/visitor/checkIn/idCard_page.dart';
import 'model/visitor_model.dart';
import 'views/visitor/query/record_details.dart';
import 'package:management/utils/channel.dart';
import 'package:management/views/mine/userInfo/change_psw.dart';

void main() async {
  try {
    WidgetsFlutterBinding.ensureInitialized();

    await FlutterDownloader.initialize();
    await SystemChrome.setEnabledSystemUIOverlays([]); //全屏
    MyLog.init(isDebug: true);
    SpUtil.getInstance();
    BluetoothChannel.init();
    await DeviceHelper.getDeviceInfo();
    runApp(FutureBuilder(
        future: VisitorProvider.provider.initDB(),
        builder: (_, snap) {
          if (snap.connectionState == ConnectionState.done) {
            return MyApp();
          } else {
            return Material(
              child: Center(
                child: FlutterLogo(
                  size: 80,
                ),
              ),
            );
          }
        }));
  } on Exception catch (e) {
    MyLog.e(e.toString());
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(1920.0, 1032.0),
      builder: () => MaterialApp(
        debugShowCheckedModeBanner: false,
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        supportedLocales: [const Locale('en', ''), const Locale('zh', '')],
        title: "管理中心",
        home: LoginPage(),
        theme: ThemeData(primarySwatch: Colors.blue),
        routes: {
          '/HomePage': (context) => HomePage(),
          '/idCardRegistration': (context) => IDCardRegistration(),
          '/visitReason': (context) => VisitReason(),
          '/VisitorRecordQuery': (context) => VisitorRecordQuery(),
          '/QueryResult': (context) => QueryResult(),
          '/RecordDetails': (context) => RecordDetails(),
          '/SearchBlt': (context) => BluetoothPage(),
          '/DeviceDetail': (context) => DeviceDetail(),
          '/PackageSignFor': (context) => PackageSignFor(),
          '/AddFaceCamera': (context) => AddFaceCamera(),
          '/LoginPage': (context) => LoginPage(),
          '/RegisterPage': (context) => RegisterPage(),
          '/UserInfo': (context) => UserInfo(),
          '/OperationRecord': (context) => OpenGateRecord(),
          '/SearchGateRecordCondition': (context) =>
              SearchGateRecordCondition(),
          '/ChangePassword': (context) => ChangePassword(),
          '/DeleteRecordByTime': (context) => DeleteRecordByTime(),
          '/SpeechRecognize': (context) => SpeechRecognize(),
          "/VideoIntercom": (context) => VideoIntercom(),
          "/AddDevice": (context) => AddDevice(),
          // "/Agora": (context) => Agora(),
          // "/CallPage": (context) => CallPage(),
          // "/IndexPage": (context) => IndexPage(),
          "/VersionInfo": (context) => VersionInfo(),
          "/Setting": (context) => Setting(),
        },
      ),
    );
  }
}
